/**
 * Create By FengZheng
 * On 2019/10/30 4:25
 */
(function ($) {
    "use strict";

    let $dom = {
        $content: $("#content")
    };


    /**
     * 查找该家长的课程信息
     */
    function queryClassInfo() {
        $.ajax({
            url: "/parents/frame/queryClassInfo",
            type: "post",
            dataType: "json",
            success: function (resultD) {
                if( (typeof resultD)==="object" ){
                    let status = resultD.status;
                    if(status === 200){
                        let info = resultD.info;

                        if( (typeof info)==="string" ){
                            info = JSON.parse(info);
                        }

                        let len = info.length;
                        if(len > 0){
                            let cStudentIdArr = [];
                            let studentArray = [];
                            for(let i=0; i<len; i++){
                                let index = cStudentIdArr.indexOf(info[i].studentId);

                                // 新的学生信息
                                if(index === -1){
                                    let classArray = [];
                                    classArray.push(info[i]);

                                    cStudentIdArr.push(info[i].studentId);
                                    studentArray.push(classArray);
                                }else{
                                    studentArray[index].push(info[i]);
                                }
                            }
                            outputClassInfo(studentArray);

                            cStudentIdArr = null;
                        }else{
                            let nullContent = "<div style='font-size: 16px;'>您的孩子还没有课程信息</div>";
                            $dom.$content.append(nullContent);
                        }
                    }
                }
            }
        });
    }

    /**
     * 遍历输出信息
     */
    function outputClassInfo(studentArray){

        let tableContent = "";

        for(let i=0,len=studentArray.length; i<len; i++){

            let classArray = studentArray[i];
            let caption,tableTr="";

            for(let j=0,cLen=classArray.length; j<cLen;j++){
                let info = classArray[j];

                // 判断学生真实姓名是否为空
                let studentName;
                if(info.studentRealName === null || info.studentRealName === ""){
                    studentName = info.studentName;
                }else{
                    studentName = info.studentRealName;
                }

                // 判断教师真实姓名是否为空
                let teacherName;
                if(info.teacherRealName === null || info.teacherRealName === ""){
                    teacherName = info.teacherName;
                }else{
                    teacherName = info.teacherRealName;
                }

                caption = "<caption style=\"color: #000;\"><span style=\"font-weight: bold;\">"+studentName+"</span>&nbsp;同学的课程信息</caption>";
                let number = "<td>"+ (parseInt(j)+1) +"</td>";
                let teacherNameTd = "<td>"+ teacherName +"</td>";
                let courseName = "<td>"+ info.subjectName +"</td>";
                let className = "<td>"+ info.className +"</td>";
                let classTime = "<td>"+ formatTime(info.classTime) +"</td>";

                let tr = "<tr>"+ number+teacherNameTd+courseName+className+classTime +"</tr>";
                tableTr = tableTr + tr;
            }

            let table = "<div class=\"row aside-container\"><div class=\"col-xs-12 col-sm-12 col-md-12 col-lg-12\"><div class=\"table-responsive\">" +
                "<table class=\"table table-bordered table-hover table-condensed\">"
                + caption +
                "<thead><tr><th>序号</th><th>老师</th><th>课程名</th><th>课时名</th><th>上课时间</th></tr></thead>" +
                "<tbody>"+ tableTr +"</tbody></div></div></div>";
            tableContent = tableContent + table;
        }

        $dom.$content.append(tableContent);
    }

    /**
     * 时间格式化
     */
    function formatTime(longTime){
        let date = new Date(longTime);

        let year = date.getFullYear(),
            month = date.getMonth()+1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes();

        month = month < 10 ? "0"+month : month;
        day = day < 10 ? "0"+day : day;
        hour = hour < 10 ? "0"+hour : hour;
        minute = minute < 10 ? "0"+minute : minute;

        return year +"-"+ month +"-"+ day +" "+ hour +":"+minute+":00";
    }

    $(document).ready(function () {
        queryClassInfo();
    });
}(window.jQuery));